package com.znzz.course.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import com.znzz.course.R;
import com.znzz.course.entity.Student;
import com.znzz.course.util.ConnByHttp;

import com.znzz.course.util.db.StudentDao;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 绑定信息模块
 */
public class BindingInformationActivity extends AppCompatActivity {
    private EditText et_student_id,et_phone;
    private Button okButton,reButton;
    Handler handler;
    String studentId,phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_binding_information);
        //点击对话框是否消失
        setFinishOnTouchOutside(false);
        initView();
        //获取控件数据信息
        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                studentId = et_student_id.getText().toString();
                phone = et_phone.getText().toString();
                initialize(studentId,phone);
            }
        });
        reButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BindingInformationActivity.this,MainActivity.class);
                intent.putExtra("msg","no");
                setResult(1,intent);
                finish();
            }
        });
        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 1:
                        //绑定信息
                        String content = msg.obj.toString();
                        JSONObject jsonObject;
                        String result = "";
                        try {
                            jsonObject = new JSONObject(content);
                            result = jsonObject.getString("result");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        if (result.equals("T")){
                            StudentDao studentDao = new StudentDao(BindingInformationActivity.this);
                            Student student = studentDao.loadData();
                            //先把学生信息封装到student对象中，然后删除数据库的student信息，在进行添加，使学生信息完善
                            if (student!=null){
                                studentDao.deleteStudent();
                            }
                            student.setStudentId(studentId);
                            student.setPhone(phone);
                            studentDao.saveData(student);
                            Intent intent = new Intent(BindingInformationActivity.this,MainActivity.class);
                            intent.putExtra("msg","ok");
                            setResult(1,intent);
                            finish();
                            Toast.makeText(BindingInformationActivity.this,"绑定成功",Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(BindingInformationActivity.this,"绑定失败",Toast.LENGTH_LONG).show();
                        }
                        break;
                }
            }
        };

    }
    public void initView(){
        et_student_id = findViewById(R.id.et_student_id);
        et_phone = findViewById(R.id.et_phone);
        okButton = findViewById(R.id.okButton);
        reButton =findViewById(R.id.reButton);
    }

    //绑定信息---通信方法
    public void initialize(String studentId,String phone){
        String url =
                "http://192.168.92.1:8123/courseServer/bindingInformation?studentId="+studentId+"&phone="+phone;
        //启动子线程
        ConnByHttp connByHttp=new ConnByHttp(url,handler);
        connByHttp.start();
    }
}